package top.javai.www.controller;


import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import top.javai.www.pojo.JobAndTrigger;

@Controller
@RequestMapping("/quartz")
public class TestController {

    @Autowired //注入了工厂中 调度器
    private Scheduler scheduler;
    //添加一个定时任务
    //方法的参数: jt=新任务的相关数据
    @RequestMapping("add")
    public String addJob(JobAndTrigger jt) throws ClassNotFoundException, SchedulerException {
        //创建JobDetail
        JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(jt.getJobClassName())).withIdentity(jt.getJobName(),
                jt.getJobGroup()).storeDurably(true).build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jt.getJobName(),jt.getJobGroup())
                .startNow().withSchedule(CronScheduleBuilder.cronSchedule(jt.getCronExpression()))
                .build();
        scheduler.scheduleJob(jobDetail,cronTrigger);
//        scheduler.start();
        return "redirect:query";
    }

}
